package com.cqupt.scanning.system.acl.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqupt.scanning.system.acl.entity.AclPermission;
import com.cqupt.scanning.system.acl.entity.AclRolePermission;
import com.cqupt.scanning.system.acl.entity.AclUser;
import com.cqupt.scanning.system.acl.entity.vo.AssignPermissionsVO;
import com.cqupt.scanning.system.acl.helper.PermissionHelper;
import com.cqupt.scanning.system.acl.mapper.AclPermissionMapper;
import com.cqupt.scanning.system.acl.service.AclPermissionService;
import com.cqupt.scanning.system.acl.service.AclRolePermissionService;
import com.cqupt.scanning.system.acl.service.AclUserService;
import com.cqupt.scanning.system.utils.result.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 权限 服务实现类
 * </p>
 *
 * @author 宋宝梁
 * @since 2021-07-05
 */
@Service
public class AclPermissionServiceImpl extends ServiceImpl<AclPermissionMapper, AclPermission> implements AclPermissionService {

    @Autowired
    private AclUserService aclUserService;

    @Autowired
    private AclRolePermissionService aclRolePermissionService;

    @Override
    public List<String> selectPermissionValueByUserId(Integer userId) {
        List<AclPermission> selectPermissionList = null;
        if(this.isSysAdmin(userId)) {
            //如果是超级管理员，获取所有权限
            selectPermissionList = baseMapper.selectList(null);
        } else {
            selectPermissionList = baseMapper.selectPermissionValueByUserId(userId);
        }
        List<String> permissionValueList = new ArrayList<>();
        for (AclPermission permission : selectPermissionList) {
            permissionValueList.add(permission.getPermissionValue());
        }
        return permissionValueList;
    }

    @Override
    public List<AclPermission> selectPermissionByUserId(Integer userId) {
        List<AclPermission> selectPermissionList = null;
        boolean isAdmin = this.isSysAdmin(userId);
        if(isAdmin) {
            //如果是超级管理员，获取所有菜单
            selectPermissionList = baseMapper.selectList(null);
            return PermissionHelper.bulid(selectPermissionList, isAdmin);
        } else {
            selectPermissionList = baseMapper.selectPermissionValueByUserId(userId);
            return PermissionHelper.bulid(selectPermissionList, isAdmin);
        }

    }

    @Override
    public R getPermissionsByRoleId(Integer roleId) {
        List<AclPermission> permissionList = baseMapper.getPermissionsByRoleId(roleId);
        return R.ok().data("permissions", permissionList);
    }


    @Override
    public R assignPermissions(AssignPermissionsVO assignPermissionsVO) {
        List<AclRolePermission> rolePermissionList = new ArrayList<>();
        for (Integer permissionId : assignPermissionsVO.getPermissionIds()) {
            AclRolePermission rolePermission = new AclRolePermission();
            rolePermission.setRoleId(assignPermissionsVO.getRoleId()).setPermissionId(permissionId);
            rolePermissionList.add(rolePermission);
        }
        boolean res = aclRolePermissionService.saveBatch(rolePermissionList, assignPermissionsVO.getPermissionIds().length);
        if (!res) R.error();
        return R.ok();
    }

    @Override
    public R getAllPermissions() {
        List<AclPermission> permissionList = baseMapper.selectList(null);
        return R.ok().data("permissions", permissionList);
    }


    /**
     * 判断用户是否系统管理员
     * @param userId
     * @return
     */
    private boolean isSysAdmin(Integer userId) {
        AclUser user = aclUserService.getById(userId);
        return null != user && "admin".equals(user.getUsername());
    }
}
